/* using array to implement a random queue */

class RandomQueue {
    constructor() {
        this.items = [];
    }

    isEmpty() {
        return !this.items.length;
    }

    size() {
        return this.items.length;
    }

    enqueue(item) {
        if (typeof item === 'undefined') {
            throw Error('No Arguments');
        }

        this.items.push(item);
    }

    /* retuurn a random item and delete it from queue */
    dequeue() {
        if (!this.items.length) {
            throw Error('Empty Queue');
        }
        let index = Math.floor(Math.random() * (this.items.length - 1));
        let tmp = this.items[index];
        this.items[index] = this.items[this.items.length - 1];
        this.items[this.items.length - 1] = tmp;
        return this.items.pop();
    }

    /* return a random element and don't delete it */
    sample(){
        if(!this.items.length){
            throw Error("Empty Queue");
        }

        let index = Math.floor(Math.random() * (this.items.length - 1));
        return this.items[index];
    }
}

module.exports = RandomQueue;
